//
//  PFScanResultViewController.m
//  PFScan
//
//  Created by perfayMini on 2018/10/25.
//  Copyright © 2018 perfayMini. All rights reserved.
//

#import "PFScanResultViewController.h"
#import "ScanResultTableViewCell.h"
#import "PFScanResultFirstTableViewCell.h"
static NSString * Identifier1 = @"HistoryResultCell1";
static NSString * Identifier2 = @"HistoryResultCell2";


@interface PFScanResultViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray * source;
@end

@implementation PFScanResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self config];
    
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
    if([self.result hasPrefix:@"http"] || [self.result hasPrefix:@"www."]){
        [self loadData];
    }
}

- (void)loadData{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.source addObject:@"安全检查"];
        [self.tableView reloadData];
    });
    
}

- (void)config{
    self.navigationItem.title = @"扫描结果";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor qmui_colorWithHexString:@"eeeeee"];
    [self.view addSubview:self.tableView];
//    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:Identifier1];
    [self.tableView registerClass:[ScanResultTableViewCell class] forCellReuseIdentifier:Identifier2];
    [self.tableView registerClass:[PFScanResultFirstTableViewCell class] forCellReuseIdentifier:Identifier1];

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kNavigation_TOP_Margin);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
}


#pragma mark -------------   UITableViewDelegate,UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.section == 0){
        PFScanResultFirstTableViewCell *  cell = [tableView dequeueReusableCellWithIdentifier:Identifier1 forIndexPath:indexPath];
        if([self.result hasPrefix:@"http"] || [self.result hasPrefix:@"www."]){
            cell.iconImageView.image = [UIImage imageNamed:@"网络"];
        }else if([self deptNumInputShouldNumber:self.result] && self.result.length == 13){
            cell.iconImageView.image = [UIImage imageNamed:@"ICON_条形码"];
        }else{
            cell.iconImageView.image = [UIImage imageNamed:@"二维码"];
        }
        [cell setTitle:self.result];
        return cell;
    }else if(indexPath.section == 1){
        ScanResultTableViewCell *  cell = [tableView dequeueReusableCellWithIdentifier:Identifier2 forIndexPath:indexPath];
        cell.iconImageView.image = [UIImage imageNamed:@"趋势"];
        cell.nameLabel.text = self.source[indexPath.section];
        int count = [[HistoryManager shareHistoryManager] historyCountWithHistory:self.result];
        cell.countLabel.text = [NSString stringWithFormat:@"%d",count];
        return cell;
    }else if(indexPath.section == 2){
        ScanResultTableViewCell *  cell = [tableView dequeueReusableCellWithIdentifier:Identifier2 forIndexPath:indexPath];
        cell.iconImageView.image = [UIImage imageNamed:@"类型"];
        cell.nameLabel.text = self.source[indexPath.section];
        if([self deptNumInputShouldNumber:self.result] && self.result.length == 13){
            cell.countLabel.text = @"条形码";
        }else{
            cell.countLabel.text = @"二维码";
        }
        return cell;
    }else if(indexPath.section == 3){
        
        ScanResultTableViewCell *  cell = [tableView dequeueReusableCellWithIdentifier:Identifier2 forIndexPath:indexPath];
        if(indexPath.row == 0){
            cell.iconImageView.image = [UIImage imageNamed:@"安全"];
            cell.nameLabel.text = self.source[indexPath.section];
            cell.countLabel.text = @"链接安全";
        }else{
            cell.iconImageView.image = [UIImage imageNamed:@"来源"];
            cell.nameLabel.text = @"来源";
            if([self.result containsString:@"wechat.com"]){
                cell.countLabel.text = @"微信";
            }else if([self.result containsString:@"m.tb.cn"]){
                cell.countLabel.text = @"淘宝";
            }else if([self.result containsString:@"baidu"]){
                cell.countLabel.text = @"百度";
            }else if([self.result containsString:@"apple"]){
                cell.countLabel.text = @"Apple";
            }else if([self.result containsString:@"qm.qq.com"]){
                cell.countLabel.text = @"QQ";
            }else if([self.result containsString:@"weixin.qq.com"]){
                cell.countLabel.text = @"微信公众号";
            }else{
                cell.countLabel.text = @"未知";
            }
        }
        return cell;
    }
    return nil;
}


- (BOOL)deptNumInputShouldNumber:(NSString *)str
{
    if (str.length == 0) {
        return NO;
    }
    NSString *regex = @"[0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    if ([pred evaluateWithObject:str]) {
        return YES;
    }
    return NO;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if(section == 3){
        return 2;
    }
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.source.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.section == 0){
       return [self.tableView cellHeightForIndexPath:indexPath model:self.result keyPath:@"title" cellClass:[PFScanResultFirstTableViewCell class] contentViewWidth:SCREEN_WIDTH];
    }
    return 50;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 20.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [[UIView alloc] init];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.section == 0){
        if([self.result hasPrefix:@"http"] || [self.result hasPrefix:@"www."] ){
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.result]];
        }
        UIPasteboard*pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = self.result;
        [QMUITips showInfo:@"成功复制到剪切板" inView:self.view hideAfterDelay:2];
    }

}


- (NSMutableArray *)source{
    if(!_source){
        NSArray * arr = @[@"名称",@"我最近扫描次数:",@"类型:"];
        _source = [[NSMutableArray alloc] initWithArray:arr];
    }
    return _source;
}


//- (NSArray *)histories{
//
//    return [[HistoryManager shareHistoryManager] histories];
//}

@end
